package com.store.framework.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.store.framework.context.SystemServletKey;
import com.store.framework.entry.AppAdmin;
import com.store.framework.entry.AppPerTbl;
import com.store.framework.gui.hk.FS;
import com.store.framework.logic.IAppAdminLogic;
import com.store.framework.logic.IAppPerLogic;
import com.store.framework.logic.imp.AppAdminLogic;
import com.store.framework.logic.imp.AppPerLogic;

/**
 * Servlet implementation class SystemServlet
 */
public class SystemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SystemServlet() {
        super();
        class SysFS implements Runnable{
			@Override
			public void run() {
				// TODO Auto-generated method stub
				FS fs = new FS();
				fs.init();
				try {
					fs.execute("hy");
				} catch (Exception e) {
				}
				fs.destroy();
			}
        }
        Thread th = new Thread(new SysFS());
        th.start();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Manage(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String mess = Manage(request,response);
		
		response.getWriter().write(mess);
	
	}

	/**
	 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Manage(request,response);
	}
	IAppPerLogic iAppPerLogic = new AppPerLogic();
	IAppAdminLogic  iAppAdminLogic = new AppAdminLogic();
	private String Manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String item = request.getParameter(SystemServletKey.KEY_ITEM);
		if(item.equals(SystemServletKey.KEY_ITEM_LOGIN)){
			String loginName = request.getParameter("loginName");					
			String password = request.getParameter("loginPassword");			 
			String message = "";
			List<AppPerTbl> allAppPer = iAppPerLogic.select();
			for(AppPerTbl apt : allAppPer){
				if(loginName.equals(apt.getPerName())){
					if(password.equals(apt.getPassword())){
						HttpSession session = request.getSession();
						session.removeAttribute(SystemServletKey.LOGIN_USER_INFO);
						session.setAttribute(SystemServletKey.LOGIN_USER_INFO,apt);
						message = "success";
					}else{
						message = "passwordError";
					}
					return message;
				}
			}
			return "userError";
		}else if(item.equals(SystemServletKey.KEY_ITEM_ADMINLOGIN)){
			String loginName = request.getParameter("loginName");					
			String password = request.getParameter("loginPassword");	
			String message = "";
			List<AppAdmin> allAppAdmin = iAppAdminLogic.select();
			for(AppAdmin apt : allAppAdmin){
				if(loginName.equals(apt.getAdName())){
					if(password.equals(apt.getAdPassword())){
						HttpSession session = request.getSession();
						session.removeAttribute(SystemServletKey.LOGIN_ADMINUSER_INFO);
						session.setAttribute(SystemServletKey.LOGIN_ADMINUSER_INFO,apt);
						message = "success";
					}else{
						message = "passwordError";
					}
					return message;
				}
			}
			return "userError";
		}
		return "itemError";
	}

}
